package org.xxpay.pay.channel.abcpay;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.MyBase64;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.ctrl.common.BaseController;
import org.xxpay.pay.util.CodeImgUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping(Constant.MCH_CONTROLLER_ROOT_PATH + "/abcpay/payment")
public class AbcPamentController extends BaseController {

    private static final MyLog _log = MyLog.getLog(AbcPamentController.class);

    /**
     * 获取二维码图片流
     */
    @RequestMapping("/qrcode_img_get")
    public void getQrCodeImg(HttpServletRequest request, HttpServletResponse response) throws IOException, WriterException {
        String url = request.getParameter("url");
        CodeImgUtil.writeQrCode(response.getOutputStream(), url, 200, 200);
    }

    /**
     * 跳转收银台页面
     */
    @RequestMapping("/pay_wap.html")
    public String toPay(HttpServletRequest request, ModelMap model) {
        JSONObject po = getJsonParam(request);
        String param = getString(po, "param");
        String paramStr = new String(MyBase64.decode(param));
        JSONObject paramObj = JSONObject.parseObject(paramStr);
        String amount = getString(paramObj, "amount");
        String payOrderId = getString(paramObj, "payOrderId");
        String imgUrl = getString(paramObj, "imgUrl");
        String payType = getString(paramObj, "payType");

        // 订单创建时间与当前时间差值(此处使用db计算，为了避免服务器和db时间有差异)
        Long expireTime = commonService.payOrderService.getOrderTimeLeft(payOrderId, AbcpayConfig.PAY_ORDER_TIME_OUT);
        if(expireTime == null || expireTime < 0) expireTime = 0l;
        model.put("amount", amount);
        model.put("payType", payType);
        model.put("expireTime", expireTime);
        model.put("payOrderId", payOrderId);
        model.put("imgUrl", imgUrl);
        return "payment/abcpay/pay_wap";
    }

    /**
     * 查询订单
     */
    @ResponseBody
    @RequestMapping("/query")
    public JSONObject query(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String payOrderId = request.getParameter("payOrderId");
        JSONObject json = new JSONObject();
        json.put("status", "1"); // 支付中
        json.put("msg", "订单支付中"); // 支付中
        //查询订单
        PayOrder order = commonService.payOrderService.findByPayOrderId(payOrderId);
        if (order == null){
            json.put("msg", "订单不存在！");
            return json;
        }
        if (order.getStatus() == PayConstant.PAY_STATUS_FAILED){
            json.put("msg", "订单已支付失败！");
            return json;
        }
        if (order.getStatus() == PayConstant.PAY_STATUS_CLOSED){
            json.put("msg", "订单已关闭！");
            return json;
        }
        if (order.getStatus() == PayConstant.PAY_STATUS_SUCCESS || order.getStatus() == PayConstant.PAY_STATUS_COMPLETE){
            json.put("status", "2"); // 支付成功
            json.put("msg", "订单支付成功"); // 支付成功
        }
        return json;
    }

}
